package com.suray.wcs.service.pojo;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.utils.ModuleType;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.commin.core.utils.StringUtils;
import com.suray.rgv.RgvSMInterface;
import com.suray.wcs.service.system.entity.RgvDB;
import com.suray.wcs.service.system.service.impl.NodeDBServiceImpl;

/**
 * 子母车
 */
public class RgvSM extends Rgv60 {

    private NodeDBServiceImpl nodeDBService = SpringContextHolder.getBean(NodeDBServiceImpl.class);

    /**
     * 子车位置
     */
    private Node sonRgvLocation;

    /**
     * 子母车接口
     */
    private RgvSMInterface rgvSMCore;

    /**
     * 每一个DB对象对应一个业务对象
     *
     * @param rgvDB
     * @param moduleType
     */
    public RgvSM(RgvDB rgvDB, RgvSMInterface rgvCore, ModuleType moduleType) {
        super(rgvDB, rgvCore, moduleType);
        this.rgvSMCore = rgvCore;
        sonRgvLocation = rgvDB.getSonLocationNode();
        rgvSMCore.setSonGroundCodeInMother(rgvDB.getSonRgvGroundCodeInMother());
    }

    public Node getSonRgvLocation() {
        return sonRgvLocation;
    }

    public void setSonRgvLocation(Node sonRgvLocation) {
        this.sonRgvLocation = sonRgvLocation;
    }

    public RgvSMInterface getRgvCore() {
        return rgvSMCore;
    }

    public boolean isRgvSonInMother() {
        return rgvSMCore.isSonRgvInMother();
    }

    /**
     * 更新小车位置
     * @param node
     */
    protected void resetLocation(Node node) {
        boolean freshSql = false;

        if (node != null) {
            if (!curLocation.equals(getRgvDB().getLocationNode())) {
                freshSql = true;
                getRgvDB().setLocation(node.toString());
            }
            this.setCurLocation(node);
        }

        if (rgvSMCore.isSonRgvInMotherNoComm()) {
            if (node != null) {
                Node sonRgvNode = new Node(node);
                if (!sonRgvNode.equals(getRgvDB().getSonLocationNode())) {
                    freshSql = true;
                    getRgvDB().setSonRgvLocation(sonRgvNode.toString());
                }
                this.setSonRgvLocation(sonRgvNode);
            }
        } else  {
            if (StringUtils.isNotEmpty(rgvSMCore.getSonRgvGroundCode())) {
                Node tempNode = nodeDBService.getNodeByGroundCode(rgvSMCore.getSonRgvGroundCode());
                if (tempNode != null) {
                    if (!tempNode.equals(getRgvDB().getSonLocationNode())) {
                        freshSql = true;
                        getRgvDB().setSonRgvLocation(tempNode.toString());
                    }
                    this.setSonRgvLocation(tempNode);
                }
            }
        }

        if (freshSql) {
            rgvDBService.updateRgvLocation(getRgvDB());
        }
    }

    public void setError(String message) {
        rgvSMCore.setError(message);
    }

    //    @Override
//    public void checkInterrupt(String msg, long checkTime) {
//        super.checkInterrupt(msg, checkTime);
//
//        if (rgvCore.getRgvStatus() == com.suray.type.RgvStatus.ERROR) {
//            LoggerUtil.error("检测到小车出现故障, In ->" + msg
//                    + ", 故障原因：" + rgvCore.getErrorInfo(), this.getClass());
//            if (rgvCore.getErrorInfo() != null) {
//                throw new OrderExecuteException(this.getRgvDB().getRgvNo(), rgvCore.getErrorInfo().getErrorCode(),
//                        rgvCore.getErrorInfo().getErrorMessage());
//            } else {
//                throw new OrderExecuteException(this.getRgvDB().getRgvNo(), 0xF000,
//                        "小车故障");
//            }
//        }
//    }
}
